﻿using Microsoft.EntityFrameworkCore;
using star_questionnair.Models;
using star_questionnair.Utilities;

namespace star_questionnair.Services.impl;

/// <summary>
/// 问题接口实现层
/// </summary>
public class QuestionService : IQuestionService
    {
        private readonly ApplicationDbContext _context;

        public QuestionService(ApplicationDbContext context)
        {
            _context = context;
        }

        public async Task<Question> GetQuestionAfterCreateAsync(Question question)
        {
            return await _context.Questions
                .FirstOrDefaultAsync(q => q.QuestionnaireId == question.QuestionnaireId &&
                                          q.Type == question.Type &&
                                          q.Content == question.Content);
        }

        public async Task InsertQuestionAsync(int questionnaireId, string type, string content)
        {
            var question = new Question
            {
                QuestionnaireId = questionnaireId,
                Type = type,
                Content = content,
                Ordination = await GetNextOrdinationAsync(questionnaireId)
            };

            _context.Questions.Add(question);
            await _context.SaveChangesAsync();
        }

        private async Task<int> GetNextOrdinationAsync(int questionnaireId)
        {
            var maxOrdination = await _context.Questions
                .Where(q => q.QuestionnaireId == questionnaireId)
                .MaxAsync(q => (int?)q.Ordination) ?? 0;

            return maxOrdination + 1;
        }

        public async Task<Question> GetQuestionByIdAsync(int questionId)
        {
            return await _context.Questions.FindAsync(questionId);
        }

        public async Task UpdateQuestionAsync(Question question)
        {
            var existingQuestion = await GetQuestionByIdAsync(question.Id);
            if (existingQuestion == null)
                throw new Exception("Question not found.");

            // Copy properties to the existing entity
            existingQuestion.Type = question.Type ?? existingQuestion.Type;
            existingQuestion.Content = question.Content ?? existingQuestion.Content;

            _context.Questions.Update(existingQuestion);
            await _context.SaveChangesAsync();
        }

        public async Task DeleteQuestionAsync(int questionId)
        {
            var question = await GetQuestionByIdAsync(questionId);
            if (question == null)
                throw new Exception("Question not found.");

            _context.Questions.Remove(question);
            await _context.SaveChangesAsync();
        }

        public async Task<List<Option>> DisplayOptionsAsync(Question question)
        {
            // Example logic to generate and return options
            return await Task.FromResult(new List<Option>
            {
                new Option { Content = "Option 1" },
                new Option { Content = "Option 2" },
                new Option { Content = "Option 3" }
            });
        }
    }